package ai.汉诺塔;

import javax.swing.*;
import java.awt.*;
import java.util.Stack;

/**
 * 汉诺塔动态演示
 */
public class UI extends JFrame {

    private Tower a, b, c;

    //主界面，用于刷新界面
    private JPanel main;

    //汉诺塔阶数
    private int count = 3;

    //内部调用的构造函数
    UI(){
        //原始盘
        a = new Tower("a", new Stack<>());

        //中转盘
        b = new Tower("b", new Stack<>());

        //目标盘
        c = new Tower("c", new Stack<>());

        for (int i = count;i>=1;i--) {
            a.getTower().push(i);
        }

        initLaunch();
        setVisible(true);
    }

    //外部调用的构造函数
    UI(Tower a, Tower b, Tower c){
        this.a = a;
        this.b = b;
        this.c = c;

        initLaunch();
        setVisible(true);
    }

    public static void main(String[] args) {
        HanoiTower hanoiTower = new HanoiTower();

        UI ui = new UI();

        //开始移动
        new Thread(new Runnable() {
            @Override
            public void run() {
                hanoiTower.move(ui.count, ui.a, ui.b, ui.c, ui);
            }
        }).start();
    }

    /**
     * 初始化界面
     */
    private void initLaunch() {
        setTitle("汉诺塔问题");
        setSize(1200, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        main = new JPanel();
        //设置布局
        GridLayout layout = new GridLayout(1, 3);
        main.setLayout(layout);
        add(main);

        freshUI();
    }

    /**
     * 刷新界面
     */
    public void freshUI(){
        main.removeAll();
        main.repaint();
        main.add(new JLabel(a.toString()));
        main.add(new JLabel(b.toString()));
        main.add(new JLabel(c.toString()));
        main.revalidate();
    }
}
